import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;

/**
 * This class represents the Main User Interface
 *
 * @author Matthew Page
 *         Date: 19/08/2010
 *         Time: 10:10:04 AM
 */

public class MainUI {
    InterGrid interGrid = new InterGrid();
    BufferedReader consoleIn = new BufferedReader(new InputStreamReader(System.in));
    int menuSelection = 0;
    private int MINRUNSIM = 1;
    private int MAXRUNSIM = 10;
    private int MINHORLANES = 1;
    private int MAXHORLANES = 3;
    private int MINVERTLANES = 1;
    private int MAXVERTLANES = 4;
    private int MINPROB = 0;
    private int MAXPROB = 1;

    /**
     * Print menu to screen
     */
    public void showMenu() {
        System.out.println("=== MENU ===\n" +
                "1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n" +
                "2. Set Number of vertical lanes (V-street) [min 1, max 4]\n" +
                "3. Set Probability of a car entering H-street [min 0, max 1]\n" +
                "4. Set Probability of a car entering V-street [min 0, max 1]\n" +
                "5. Run one simulation cycle\n" +
                "6. Set and run number of simulation cycles [min 1, max 10]\n" +
                "7. Quit\n" +
                "Enter your choice>");
    }

    /**
     * Controls elements of the program.
     */
    public void programControl() {
        interGrid.initiateGrid();
        interGrid.createLaneBoundaries();
        interGrid.displayGrid();

        while (menuSelection != 7) {
            boolean inputCheck = false;
            showMenu();
            menuSelection = getMenuSelection();
            switch (menuSelection) {
                case 1:
                    while (inputCheck == false) { //Continually asks user for input until a valid input is entered.
                        System.out.println("How many horizontal lanes would you like? Min " + MINHORLANES + ", Max " + MAXHORLANES);
                        String userInput = "";
                        int intUserInput = 0;
                        try {
                            userInput = consoleIn.readLine(); //try getting user input
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            intUserInput = Integer.parseInt(userInput);
                            inputCheck = interGrid.setNumHorLanes(intUserInput);
                        } catch (NumberFormatException e) {
                            inputCheck = false;
                        }

                        if (inputCheck == false) {
                            System.out.println("Invalid Input");
                        } else {
                            interGrid.initiateGrid();
                            interGrid.createLaneBoundaries();
                        }
                    }
                    break;
                case 2:
                    while (inputCheck == false) { //Continually asks user for input until a valid input is entered.
                        System.out.println("How many vertical lanes would you like? Min 1, Max 4");
                        String userInput = "";
                        int intUserInput = 0;
                        try {
                            userInput = consoleIn.readLine();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            intUserInput = Integer.parseInt(userInput);
                            inputCheck = interGrid.setNumVertLanes(intUserInput);
                        } catch (NumberFormatException e) {
                            inputCheck = false;
                        }

                        if (inputCheck == false) {
                            System.out.println("Invalid Input");
                        } else {
                            interGrid.initiateGrid();
                            interGrid.createLaneBoundaries();
                        }
                    }
                    break;
                case 3:
                    while (inputCheck == false) { //Continually asks user for input until a valid input is entered.
                        System.out.println("What is the probability of a car entering horizontal street? Min 0, Max 1");
                        String userInput = "";
                        double doubleUserInput = 0.0;
                        try {
                            userInput = consoleIn.readLine();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            doubleUserInput = Double.parseDouble(userInput);
                            inputCheck = interGrid.setHorProbability(doubleUserInput);
                        } catch (NumberFormatException e) {
                            inputCheck = false;
                        }

                        if (inputCheck == false) {
                            System.out.println("Invalid Input");
                        }
                    }
                    break;
                case 4:
                    while (inputCheck == false) { //Continually asks user for input until a valid input is entered.
                        System.out.println("What is the probability of a car entering vertical street? Min 0, Max 1");
                        String userInput = "";
                        double doubleUserInput = 0.0;
                        try {
                            userInput = consoleIn.readLine();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            doubleUserInput = Double.parseDouble(userInput);
                            inputCheck = interGrid.setVertProbability(doubleUserInput);
                        } catch (NumberFormatException e) {
                            inputCheck = false;
                        }

                        if (inputCheck == false) {
                            System.out.println("Invalid Input");
                        }
                    }
                    break;
                case 5:
                    try {
                        interGrid.runSimulation(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
                case 6:
                    while (inputCheck == false) { //Continually asks user for input until a valid input is entered.
                        System.out.println("How many simulations would you like to run? Min 1, Max 10");
                        String userInput = "";
                        int intUserInput = 0;
                        try {
                            userInput = consoleIn.readLine();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            intUserInput = Integer.parseInt(userInput);
                            if (intUserInput >= MINRUNSIM && intUserInput <= MAXRUNSIM) {
                                inputCheck = true;
                            } else {
                                inputCheck = false;
                            }

                        } catch (NumberFormatException e) {
                            inputCheck = false;
                        }

                        if (inputCheck == false) {
                            System.out.println("Invalid Input");
                        } else {
                            try {
                                interGrid.runSimulation(intUserInput);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
                case 7:
                    System.out.println("Thank you for using my Traffic Simulator! :)");
                    break;
                case 8:
                    System.out.println("Invalid Input");
                    break;
            }

        }
    }

    /**
     * Get user selection for menu
     *
     * @return integer between 1 and 8 inclusive. 1 to 7 are selections, 8 is invalid input.
     */
    public int getMenuSelection() {
        BufferedReader consoleIn = new BufferedReader(new InputStreamReader(System.in));
        String userInput = "8";
        try {
            userInput = consoleIn.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int choice = 0;
        try {
            choice = Integer.parseInt(userInput);
        } catch (NumberFormatException e) {
            choice = 8;
        }
        if ((choice > 0) && (choice < 8)) {
            return choice;
        } else {
            return 8;
        }
    }

}